//
//  XFXAFNAppTools.m
//  XFXnews
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXAFNAppTools.h"
#import "AFNetworking.h"
@implementation XFXAFNAppTools

+ (void)getNewsWithHeadline:(NSString *)string block:(AppToolsBlock)block error:(AppToolsBlockError)blockerror{
    // 检查网络状态管理者 是单例
    AFNetworkReachabilityManager *netWorkManger = [AFNetworkReachabilityManager sharedManager];
    
    // 如果要监测网络状态的变化,必须用检测管理器的单例的startMonitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    NSString *doucumentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSLog(@"%@",doucumentPath);
    NSString *path = [NSString stringWithFormat:@"%ld.plist",[string hash]];
    NSString *textFilPath = [doucumentPath
                             stringByAppendingPathComponent:path];
    
    // 检测网络连接的单例,网络变化时的回调方法
    
    [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        
    }];
    
    NSString *str = [NSString stringWithFormat:@"%@",string];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    [manger GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManger stopMonitoring];
        
        
        block (responseObject);
        
        BOOL result = [responseObject writeToFile:textFilPath atomically:YES];
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:textFilPath];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        blockerror (error);
        id result = [NSMutableDictionary dictionaryWithContentsOfFile:textFilPath];
        block (result);
    }];
    
    
    
}


@end
